<?php

class Mod_card extends Model {

    function mod_card() {
        parent::Model();
    }

    function index() {
        $this->load->view('master');
    }

//===================this is function for adding new card======================//

    function add() {
        $name = $this->input->post('txt_cardname');
        $pricek = $this->input->post('txt_k');
        $pricev = $this->input->post('txt_v');
        $priceother = $this->input->post('txt_tcm');
        $description = $this->input->post('txtdetail');
        $quantity_card = $this->input->post('txt_quantity');
        $idprice = $this->input->post('card_type');
        //$get_priceid = $this->db->insert_id();
        $data = array(
            'car_type_id' => $idprice,
            'car_name' => $name,
            'car_description' => $description,
            'car_quantity' => $quantity_card,
            'car_status' => 1
                // 'price_id'=>$get_priceid,
        );
        $this->db->insert('tbl_card', $data);
        $cid = $this->db->insert_id();
        $data = array(
            'car_id' => $cid,
            'price_K' => $pricek,
            'price_V' => $pricev,
            'price_other' => $priceother,
        );
        $this->db->insert('tbl_pricecard', $data);
    }

    //==========this is function edit of card====================//

    function edit() {
        $data1 = array(
            'car_name' => $this->input->post('txt_name'),
            'car_type_id' => $this->input->post('card_type'),
            'car_quantity' => $this->input->post('txt_quantity'),
            'car_description' => $this->input->post('txt_description'),
        );
        $this->db->where('car_id', $this->uri->segment(3));
        $this->db->update('tbl_card', $data1);

        $data2 = array(
            'price_K' => $this->input->post('txt_pricek'),
            'price_V' => $this->input->post('txt_pricev'),
            'price_other' => $this->input->post('txt_price_other'),
        );
        $this->db->where('car_id', $this->uri->segment(3));
        $this->db->update('tbl_pricecard', $data2);
    }

    //=============this is function for select data to view=====================//

    function select() {
        //  return $this->db->query('SELECT * from tbl_card inner join tbl_pricecard on tbl_card.car_id=tbl_pricecard.car_id join tbl_car_type on tbl_car_type.car_type_id=tbl_card.car_type_id');

        $this->db->select('*');
        $this->db->from('tbl_card');
        $this->db->join('tbl_pricecard', 'tbl_pricecard.car_id=tbl_card.car_id');
        $this->db->join('tbl_car_type', 'tbl_car_type.car_type_id=tbl_card.car_type_id');
        return $this->db->get();
    }

    function get_cards($p=null) { // $p is array
        if ($this->input->post($this->dbf->car_name) && $this->input->post($this->dbf->car_name) != '')
            $this->db->like($this->dbf->car_name, $this->input->post($this->dbf->car_name));
        $this->db->select('*');
        $this->db->from('tbl_card');
        $this->db->limit($p[0], $p[1]);
        $this->db->where('car_status', 1);
        //   $this->db->order_by('car_id','desc');
        $this->db->join('tbl_pricecard', 'tbl_pricecard.car_id=tbl_card.car_id');
        $this->db->join('tbl_car_type', 'tbl_car_type.car_type_id=tbl_card.car_type_id');
        return $this->db->get();
    }

    //===========this is function for delete card=================//

    function delete() {
        $id = $this->uri->segment('3');
        $this->db->delete('tbl_card', array('car_id' => $id));
        $this->db->delete('tbl_pricecard', array('pri_id' => $id));
    }

    //===================This function is to add new type card to table tbl_car_type==============//

    function addtype() {
        $data = array(
            'car_type_name' => $this->input->post('type_name'),
            'car_type_description' => $this->input->post('txt_detail'),
        );
        if ($this->db->insert('tbl_car_type', $data)) {
            return true;
        }else
            return false;
    }

    //add card type 
    function add_car_type() {
        $card_type = array(
            'car_type_name' => $this->input->post('type_name'),
            'car_type_description' => $this->input->post('txt_detail')
        );

        if ($this->db->insert('tbl_car_type', $card_type)) {
            return true;
        } else {
            return false;
        }
    }

    //delete card type
    function delete_add_type() {
        $del_addtype = $this->uri->segment('3');
        $this->session->set_userdata('delcard_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Deleted Successful!</li></ul></dd></dl>');
        $this->db->delete('tbl_car_type', array('car_type_id' => $del_addtype));
    }

    //edit card type
    function edit_type_card() {
        $edit_cardtype = array(
            'car_type_name' => $this->input->post('type_name'),
            'car_type_description' => $this->input->post('txt_detail'),
        );
        $this->db->where('car_type_id',$this->uri->segment(3));
        $this->db->update('tbl_car_type',$edit_cardtype);
    }
    //Select type card  to edit 
    function select_edit(){
     
                   $get_seg=$this->uri->segment(3);
                    $this->db->where('car_type_id',$get_seg);
                    return $this->db->get('tbl_car_type');
    }
}


